<?php
/**
 * Created by JetBrains PhpStorm.
 * User: porterzhang
 * Date: 13-3-5 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        global $_G;
        //判断是否有用户登录，如果已经登录则返回已经登录用户信息
        if ($_G['cloud_uid']) {
            $variable = array(
                'messagestr' => lang('plugin/cloud', '1018'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        //判断传入的密码
        if (!$_POST['password'] || $_POST['password'] != addslashes($_POST['password'])) {
            self::setMessage(1016);
        }
        //得到登录用户信息
        $request = self::userlogin($_POST['name'], $_POST['password'], $_G['clientip'], $_POST['caid'],$_POST['areaId']);
        $variable = array(
            'messagestr' => lang('plugin/cloud', '1013'),
        );
        cloud_core::result(cloud_core::variable($variable));

    }

    /**
     * 根据传入的数据查询登录用户
     * @param $username
     * @param $password
     * @param $ip
     * @param $caid
     */
    function userlogin($username, $password, $ip, $caid,$areaid) {
        $user = C::t('#cloud#cloud_users')->fetch_first_by_name($username, $caid);
        //当查询出用户的信息不存在时
        if (empty($user)) {
            self::setMessage(1016);
        }
        //当查询的状态不可用
        if ($user['status'] == 0) {
            self::setMessage(1017);
        }
        //当查询的管理员密码错误时候
        if (md5(md5($password) . $user['salt']) != $user['password']) {
            self::setMessage(1016);
        }
        //设置用户登录状态
        //1.得到应用
        $app = C::t('#cloud#cloud_app')->fetch($user['caid']);
        $user['app'] = $app;
        //将信息写入到cookie中
        plugin_cloud::setLoginStatus($user, 'user', $areaid);
        C::t('#cloud#cloud_users')->update($user['cuid'], array('lasttime' => TIMESTAMP, 'lastip' => $ip));
    }

    /**
     * 返回错误信息
     * @param $code
     */
    function setMessage($code) {
        $variable = array(
            'code' => $code,
            'messagestr' => lang('plugin/cloud', '' . $code),
        );
        cloud_core::result(cloud_core::variable($variable));
    }

}

?>

